﻿using LY.Entity;
using LY.Entity.Entities;
using LY.Entity.PageSearch;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LY.DataAccess.Repository
{
    public class ModuleRepository : BaseRepository
    {
        public PagedList<S_Module> QueryModuleList(PageView view)
        {
            string cols = @" [ID]
      ,[Name],[Url],Sequence,layer 
       ";

            return base.PageGet<S_Module>(view, cols,
                @"S_Module "
                , " and id<>1 "
                , " Path  ",
                "");
                //, " Path + CONVERT(nvarchar(20), ID + ISNULL(Sequence, 0) * 10000 + 100000000)  ",

        }

        /// <summary>
        /// 新用户模块列表
        /// </summary>
        /// <returns></returns>
        public List<S_Module_EX> QueryNewRoleModuleList()
        {
            string sql = @" SELECT [ID]
      ,[Name]
      ,[SampleName]
      ,[ParentID]
      ,[Layer]
      ,[Path]
      ,[Status]
      ,0 as UserRoleID
  FROM [S_Module] where id<>1 order by Path ";
            //return base.Query<S_Module>(sql, new { UserRoleID = roleid });
            return base.Query<S_Module_EX>(sql, null);
        }

        /// <summary>
        /// 检索模块列表
        /// </summary>
        /// <returns></returns>
        public List<S_Module_EX> QueryRoleModuleList(int roleid)
        {
            string sql = @" SELECT [ID]
      ,[Name]
      ,[SampleName]
      ,[ParentID]
      ,[Layer]
      ,[Path]
      ,[Status]
      ,isnull(b.UserRoleID,0) as UserRoleID
  FROM [S_Module] a LEFT OUTER JOIN [S_ModuleAccess] b on a.ID  = b.ModuleID and b.UserRoleID = @UserRoleID where id<>1 order by a.Path ";
            return base.Query<S_Module_EX>(sql, new { UserRoleID = roleid });
            //return base.Query<S_Module>(sql, null);
        }

        /// <summary>
        /// 检索模块列表
        /// </summary>
        /// <returns></returns>
        public List<S_Module> QueryUserModuleList()
        {
            string sql = @" SELECT *
                       FROM [S_Module] order by path ";
            //return base.Query<S_Module>(sql, new { ID = id });
            return base.Query<S_Module>(sql,null);
        }

        public List<S_Module> GetUserAccessList(int? id)
        {
//            string sql = @" SELECT * FROM [S_Module] where ID in ( select [ModuleID] from [S_ModuleAccess] where UserRoleID in (
//  select roleid from S_RoleUser where UserID in ( select ID from S_User where ID = @ID)))  order by  Path + CONVERT(nvarchar(20), ID + ISNULL(Sequence, 0) * 10000 + 100000000)  ";
            string sql = @" SELECT * FROM [S_Module] where ID in ( select [ModuleID] from [S_ModuleAccess] where UserRoleID in (
  select roleid from S_RoleUser where UserID in ( select ID from S_User where ID = @ID)))  order by  Path  ";
            return base.Query<S_Module>(sql, new { ID = id });
        }

        public List<S_Module> GetUpperModuleList(int? id)
        {
            string sql = @" SELECT *
                       FROM [S_Module] WHERE [ParentID]=@ID";
            return base.Query<S_Module>(sql, new { ID = id });
        }

        /// <summary>
        /// 删除模块信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteModule(int id)
        {
            string sql = " DELETE FROM [S_Module] WHERE ID=@ID";
            return base.Excute(sql, new { ID = id });
        }

        /// <summary>
        /// 更新模块信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int UpdateSequence(string oldpath, string newpath)
        {
            string sql = "  Update [S_Module] set path = REPLACE(path,@oldpath,@newpath) WHERE Path like @oldpath + '%'  ";
            return base.Excute(sql, new { oldpath = oldpath, newpath = newpath });
        }

        /// <summary>
        /// 检索模块信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public S_Module GetModule(int? id)
        {
            string sql = @" SELECT *
                       FROM [S_Module] WHERE Id=@ID";
            return base.Get<S_Module>(sql, new { ID = id });
        }

        /// <summary>
        /// 检索模块信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<S_Module> GetModuleSub(int? id)
        {
            string sql = @" SELECT ID
                       FROM [S_Module] WHERE Path like (SELECT Path+'%'
                       FROM [S_Module] WHERE Id=@ID)";
            return base.Query<S_Module>(sql, new { ID = id });

        }


    }
}
